const express = require('express');
const pool = require('../utils/pool')
let router = express.Router();
router.get('/FuzzySearch',(req,res)=>{
	var obj= req.query
	let sql = 'select * from recycleorder'
	let params = []
	
	// 只有当status不为空时才添加模糊查询条件
	if(obj.status && obj.status.trim() !== '' && obj.status !== '3') {
		sql += ' where status LIKE ?'
		params.push(`%${obj.status}%`)
	}
	
	pool.query(sql, params, (x,y)=>{
		if(x){
			throw x
		}
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
	})
})
router.get('/orderSearch',(req,res)=>{
	var obj= req.query
	pool.query('select*from recycleorder',(x,y)=>{
		if(x){
			throw x
		}
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
	})
})
router.get('/orderStatusSearch',(req,res)=>{
	var obj= req.query
	pool.query('select*from recycleorder where userId = ? and status = ?',[obj.userId,obj.status],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/orderAdminSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from recycleorder where  status = ?',[obj.status],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/orderStatus',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from recycleorder where status = 0',(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/orderIdSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from recycleorder where id = ?',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.post('/orderAdd',(req,res)=>{
  let obj = req.body
	pool.query('insert into recycleorder set ?',[obj],(x,y)=>{
		if(x){
			throw x;
		}
		res.send({code:200,msg:'注册成功'})
	})
 
});
router.post('/orderUpdate',(req,res)=>{
	var obj = req.body
	pool.query('update recycleorder set status = ? where id = ?',[obj.status,obj.id],(x,y)=>{
		if(x){
			throw x
		}
		if(y.affectedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功',date:obj})
		}
		
	})
	
})
router.post('/orderAllUpdate',(req,res)=>{
	var obj = req.body
	console.log(obj);
	pool.query('update recycleorder set  ? where id = ?',[obj,obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功',date:obj})
		}
		
	})
	
})
router.get('/orderDelete',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('delete from recycleorder where id = ? ',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'不存在'})
		}else{
			res.send({code:200,msg:'删除成功'})
		}
		
	})
	
})
module.exports=router;